package com.fingersoft.feature.signpad

import android.app.Application
import android.content.Context
import cn.fingersoft.eben.ui.EBenSignActivity
import cn.fingersoft.signpad.IActivitySignPadBehavior
import cn.fingersoft.signpad.ISignPadConfig
import cn.fingersoft.signpad.SignPadContext
import cn.fingersoft.signpad.api.ISignPadApi
import cn.fingersoft.signpad.api.ISignPadCallback
import cn.fingersoft.signpad.api.SignPadApi
import com.fingersoft.im.BuildConfig
import com.fingersoft.im.api.UserAgent
import com.fingersoft.im.utils.AppUtils
import com.fingersoft.im.utils.JSONUtils
import com.lzy.okgo.OkGo
import okhttp3.Call
import okhttp3.Response
import java.io.File

class SignPadConfig : ISignPadConfig {
    override fun startEbenActivity(context: Context, behavior: IActivitySignPadBehavior) {
        EBenSignActivity.startActivity(context,behavior)
    }
    override fun startEbenActivity(context: Context) {
        EBenSignActivity.startActivity(context)
    }
    override val EBenHandwritingEnabled: Boolean
        get() = AppUtils.EBenHandwritingEnabled()
    override val copyRight: String
        get() = ""
    override val currentUserInfo: String
        get() = AppUtils.getUser().userId ?: ""
    override val sign: String
        get() = AppUtils.getString("SIGN", "")

}
class SealSignPadContext {
    companion object {
        fun init(application: Application) {
            SignPadContext.init(application, SignPadConfig())
            SignPadApi.init(object : ISignPadApi {
                override fun removeSignature(callback: ISignPadCallback) {
                    OkGo.post(AppUtils.getApiUrl("i/user/deleteSignature", AppUtils.J_ECODE))
                            .headers("Emp-Info", this@Companion.getEmpInfoHeader()).execute(object : JsonCallback<SignPadResponse>(SignPadResponse::class.java) {
                                /** 对返回数据进行操作的回调， UI线程  */
                                override fun onSuccess(t: SignPadResponse?, call: Call?, response: Response?) {
                                    if (t != null) {
                                        if (t.code == 200) {
                                            callback.onSuccess(t.data)
                                        } else {
                                            callback.onError(Exception("response is error:${t.code},error:${t.msg}"))
                                        }

                                    } else {
                                        callback.onError(Exception("response is null"))
                                    }
                                }

                                override fun onError(call: Call?, response: Response?, e: java.lang.Exception?) {
                                    super.onError(call, response, e)
                                    callback.onError(e ?: Exception("unkown"))
                                }
                            })
                }

                override fun getSignature(callback: ISignPadCallback) {

                    OkGo.get(AppUtils.getApiUrl("i/user/getSignature", AppUtils.J_ECODE))
                            .headers("Emp-Info", this@Companion.getEmpInfoHeader()).execute(object : JsonCallback<SignPadResponse>(SignPadResponse::class.java) {
                                /** 对返回数据进行操作的回调， UI线程  */
                                override fun onSuccess(t: SignPadResponse?, call: Call?, response: Response?) {
                                    if (t != null) {
                                        if (t.code == 200) {
                                            callback.onSuccess(t.data)
                                        } else {
                                            callback.onError(Exception("response is error:${t.code},error:${t.msg}"))
                                        }

                                    } else {
                                        callback.onError(Exception("response is null"))
                                    }
                                }

                                override fun onError(call: Call?, response: Response?, e: java.lang.Exception?) {
                                    super.onError(call, response, e)
                                    callback.onError(e ?: Exception("unkown"))
                                }
                            })
                }

                override fun postSignature(path: String?, callback: ISignPadCallback) {
                    OkGo.post(AppUtils.getApiUrl("i/user/uploadSignature", AppUtils.J_ECODE))
                            .tag(this)
                            .headers("Emp-Info", this@Companion.getEmpInfoHeader())
                            .params("file", File(path))
                            .execute(object : JsonCallback<SignPadResponse>(SignPadResponse::class.java) {
                                /** 对返回数据进行操作的回调， UI线程  */
                                override fun onSuccess(t: SignPadResponse?, call: Call?, response: Response?) {
                                    if (t != null) {
                                        if (t.code == 200) {
                                            callback.onSuccess(t.data)
                                        } else {
                                            callback.onError(Exception("response is error:${t.code},error:${t.msg}"))
                                        }
                                    } else {
                                        callback.onError(Exception("response is null"))
                                    }
                                }
                            })
                }
            })
        }

        private fun getEmpInfoHeader() =
                JSONUtils.toJsonString(UserAgent(AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did))
    }
}
